package com.application.lucky.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.adapter.GroupCenterUserAdapter;
import com.application.lucky.bean.eventbean.LeaveRoomEvent;
import com.application.lucky.bean.jsonBean.GroupUserLIstbean;
import com.application.lucky.bean.jsonBean.RoomInfoBean;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.EventBusUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class GroupCenterActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private RecyclerView mRclView;
    private GroupCenterUserAdapter centerUserAdapter;
    private String id;
    private TextView mTvGroupCenterMoreUser;
    private TextView mTvGroupCenterId;
    private TextView mTvGroupCenterName;
    private TextView mTvGroupCenterTime;
    private TextView mTvGroupCenterWanfa;
    private TextView mTvGroupCenterQungui;
    private TextView mTvGroupCenterNoti;
    private Switch mSwitchGroupCenterMessage;
    private Button mBtGroupCenterDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public void beforeInit() {
        id = getIntent().getStringExtra("id");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_group_center;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        mRclView = findViewById(R.id.rcl_view);
        mRclView.setLayoutManager(new GridLayoutManager(this, 5));
        centerUserAdapter = new GroupCenterUserAdapter(R.layout.item_group_center_user);
        mRclView.setAdapter(centerUserAdapter);
        centerUserAdapter.addFooterView(getFooterView());
        loadData();

    }

    private List<GroupUserLIstbean> data;

    private void loadData() {
        Serializable data = getIntent().getSerializableExtra("data");
        RoomInfoBean infoBean = (RoomInfoBean) data;
        setui(infoBean);

    }

    private void setui(RoomInfoBean dataInfo) {
        if (dataInfo != null) {
            if (mTvGroupCenterId != null) {
                mTvGroupCenterId.setText(dataInfo.getId() + "");
            }
            if (mTvGroupCenterName != null) {
                mTvGroupCenterName.setText(dataInfo.getRoomName());
            }
            if (mTvGroupCenterQungui != null) {
                mTvGroupCenterQungui.setText(dataInfo.getRoomDesc());
            }
            if (mTvGroupCenterTime != null) {
                mTvGroupCenterTime.setText(dataInfo.getStartHour() + "-" + dataInfo.getEndHour());
            }
            if (mTvGroupCenterWanfa != null) {
                mTvGroupCenterWanfa.setText(dataInfo.getRoomPlayDesc());
            }

            loadList(id);
        }

    }

    private void loadList(String id) {
        new RedPRequest().relation_group_list(
                id, new CallBackEx<BaseResponse<List<GroupUserLIstbean>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<GroupUserLIstbean>> listBaseResponse) {
                        data = listBaseResponse.dataInfo;
                        if (data != null) {

                            mTvGroupCenterMoreUser.setText("查看全部成员（" + data.size() + "/500)");
                            if (data.size() < 15) {
                                if (mTvGroupCenterMoreUser != null) {
                                    mTvGroupCenterMoreUser.setVisibility(View.GONE);
                                }
                                centerUserAdapter.setNewData(data);
                            } else {
                                for (int i = 0; i < 15; i++) {
                                    centerUserAdapter.addData(data.get(i));
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailed(int code) {

                    }
                });
    }

    private View getFooterView() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_group_centet_footerview, null);

        mTvGroupCenterMoreUser = view.findViewById(R.id.tv_group_center_more_user);
        mTvGroupCenterId = view.findViewById(R.id.tv_group_center_id);
        mTvGroupCenterName = view.findViewById(R.id.tv_group_center_name);
        mTvGroupCenterTime = view.findViewById(R.id.tv_group_center_time);
        mTvGroupCenterWanfa = view.findViewById(R.id.tv_group_center_wanfa);
        mTvGroupCenterWanfa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        mTvGroupCenterQungui = view.findViewById(R.id.tv_group_center_qungui);
        mTvGroupCenterNoti = view.findViewById(R.id.tv_group_center_noti);
        mSwitchGroupCenterMessage = view.findViewById(R.id.switch_group_center_message);
        mSwitchGroupCenterMessage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO
            }
        });
        mBtGroupCenterDelete = view.findViewById(R.id.bt_group_center_delete);
        mBtGroupCenterDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBusUtils.post(new LeaveRoomEvent());
                finishAnim();
                new RedPRequest().relation_group_exit(id, new CallBackEx<BaseResponse<String>>() {
                    @Override
                    public void onSuccess(BaseResponse<String> stringBaseResponse) {

                    }

                    @Override
                    public void onFailed(int code) {

                    }
                });
            }
        });
        mTvGroupCenterMoreUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (centerUserAdapter.getData().size() == 15) {
                    centerUserAdapter.setNewData(data);
                } else {
                    List dd = new ArrayList();
                    for (int i = 0; i < 15; i++) {
                        dd.add(data.get(i));

                    }
                    centerUserAdapter.setNewData(dd);
                }

            }
        });
        return view;
    }
}
